import time,random,sys
from selenium.webdriver.common.keys import Keys  
class Event:
    def __init__(self,cfg={}):
        self.cfg = cfg
        return;
    #点击百度百科
    def baike(self):
        driver = self.cfg['driver']
        try:
            driver.find_element_by_partial_link_text('百度百科').click()
            print('【触发事件】：打开百度百科 ');
            time.sleep(6)   
             #跳转回百度标签
            handles = driver.window_handles
            driver.switch_to_window(handles[0])
            time.sleep(3)                           
        except:
            return;
        return;
    #上下滚动
    def upDown(self):
        randTimes = random.randint(2, 5)#滚动随机幅度
        print('【触发事件】：滚动 '+str(randTimes)+'下');
        driver = self.cfg['driver']
        curTimes = 0;
        try:
            #有时候找不到body
            while(curTimes<randTimes):
                curTimes = curTimes+1
                driver.find_element_by_xpath("//body").send_keys(Keys.DOWN)  
                time.sleep(1)
            curTimes = 0;   
            while(curTimes<randTimes):
                curTimes = curTimes+1
                driver.find_element_by_xpath("//body").send_keys(Keys.UP)  
                time.sleep(1)
        except:
           return; 
    #翻页回退
    def pageTurning(self):
        randTimes = random.randint(1, 3)#滚动随机幅度
        driver = self.cfg['driver']
        if(self.cfg.get('seo')):
            seo = self.cfg['seo']
        else:
            seo = 'baidu'
        curTimes = 0;
        print('【触发事件】：翻页 '+str(randTimes)+'页');
        while(curTimes<randTimes):
            curTimes = curTimes+1
            print('翻')
            #driver.find_element_by_xpath("//a[@class='n']").click();
            try:
                if(seo=='sogou'):
                    driver.find_element_by_id('sogou_next').click()
                elif(seo=='baidu'):
                    driver.find_element_by_link_text('下一页>').click()
                elif(seo=='so360'):
                    driver.find_element_by_link_text('下一页').click()
                elif(seo=='baidu_mobile'):
                    try:
                        driver.find_element_by_css_selector('.new-pagenav a.new-nextpage-only').click();                    #driver.find_element_by_css_selector('a.new-nextpage-only').click();
                    except:
                        try:
                            driver.find_element_by_css_selector('a.new-nextpage').click();
                        except:
                            driver.back();
                            time.sleep(1)
                            try:
                                driver.find_element_by_css_selector('.new-pagenav a.new-nextpage-only').click();
                            except: 
                                driver.find_element_by_css_selector('a.new-nextpage').click();
            except:
                #超过所有页数了
                randTimes = curTimes;
                break;
                
            time.sleep(5)
        curTimes = 0;   
        while(curTimes<randTimes):
            curTimes = curTimes+1
            print('回')
            try:
                if(seo=='sogou'):
                    driver.find_element_by_id('sogou_prev').click()
                elif(seo=='baidu'):
                    driver.find_element_by_link_text('<上一页').click()
                elif(seo=='so360'):
                    driver.find_element_by_link_text('上一页').click()
                elif(seo=='baidu_mobile'):
                    try:
                        driver.find_element_by_css_selector('a.new-prepage').click();
                    except:
                        driver.back();
                        time.sleep(1)
                        driver.find_element_by_css_selector('a.new-prepage').click();
            except:
                break;
            time.sleep(5)
    #随机事件
    #type 事件类型
    def random(self,type=0):
        if(type==1):
            randTimes = random.randint(2, 3)#不要百度百科
        else:
            randTimes = random.randint(1, 3)#随机幅度
        if(randTimes==1):
            self.baike()
        elif(randTimes==2):
            self.upDown()
        elif(randTimes==3):
            self.pageTurning()
        
